﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

using GrimorioApplication.Model;

namespace GrimorioApplication.Views
{
    public partial class ListRecipes : PhoneApplicationPage
    {
        public ListRecipes()
        {
            InitializeComponent();
            ReadRecipes();
            CheckIfDefaultExist();
        }

        // Carico la lista delle ricette dall'obsCollection AllRecipes.
        public void ReadRecipes()
        {
            listSelectorOfRecipes.ItemsSource = App.ViewModel.AllRecipes;
        }

        // Selezione dell'elemento nella lista: Invio a viewRecipeDetails + id della ricetta da selezionare
        private void listSelectorOfRecipes_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            DataRecipe idOfRecipeSelected = (DataRecipe)listSelectorOfRecipes.SelectedItem;
            if(idOfRecipeSelected != null)
                NavigationService.Navigate(new Uri("/Views/ViewRecipeDetails.xaml?" + "ID=" + idOfRecipeSelected.RecipeId.ToString(), UriKind.Relative));
        }

        public void CheckIfDefaultExist()
        {
            DataRecipe checkIfTempExist = App.ViewModel.getRecipeWithSelectedId(App.ViewModel.getIdRecipeWithDefaultName());
            if (checkIfTempExist != null)
            {
                if (checkIfTempExist.RecipeName == "Grimorio_TeMpOrArY_Recipe")
                {
                    App.ViewModel.DeleteRecipe(checkIfTempExist);
                }
            }
        }
    }
}